﻿var IN = new Array();
IN['lat'] = 0.000000;
IN['lng'] = 0.000000;
IN['zoom'] = 2;
var LatLng = new GLatLng(IN['lat'], IN['lng']);
var map = null; var mgr = null, mgrStunt=null,mgrPlatform=null, mgrWeapon=null,mgrVehicle=null,mgrCharacter=null;
var data;

function initialize() {
	if (GBrowserIsCompatible()) {
		var Layer1 = new GTileLayer(new GCopyrightCollection(""), 1, 5);
		Layer1.getCopyright = function(a, b) {
			return { prefix: "<div style='float:left;'><strong>map of GTA4</strong> by <a href='###' title='' rel='external nofollow'>Li-Z</a> / ", copyrightTexts: [" &copy; 2010</div>"] };
		}
		Layer1.getOpacity = function() { return 1.0; }
		Layer1.isPng = function() { return true; }

		Layer1.getTileUrl = function(tile, z) {			
			var len = Math.pow(2, z);
			if ((tile.x > len-1) || (tile.y > len-1)) return "";
			var prefix_id = "";
			var tile_id = (tile.y * len) + tile.x + 1;
			if (tile_id < 10) prefix_id = "0";
			return "images/"+ z + "_" + prefix_id + tile_id + ".jpg";
		};
		var Map1 = new GMapType(
						[Layer1],
						createCustomProjection(),
						"gta4",
						{ errorMessage: " " }
					);

		map=new GMap2(document.getElementById("mapbar"),{ mapTypes: [Map1],backgroundColor: "#5d7c8e" });
		//map.addControl(new GSmallMapControl());
		map.addControl(new GLargeMapControl3D());
		var str=window.location.search;		
		if(str!=""){
			str=str.slice(1,str.length);
			str=str.split("&");
			var lat=str[0];
			lat=lat.split("=");
			lat=lat[1]*1;
			var lng=str[1];
			lng=lng.split("=");
			lng=lng[1]*1;	
			LatLng=new GLatLng(lat,lng);
			var zoom=str[2].split("=");
			zoom=zoom[1];
			IN['zoom']=zoom;
		}
		map.setCenter(LatLng,IN['zoom']);
		map.setZoom(IN['zoom']);
		map.panTo(LatLng);
		map.addControl(new GOverviewMapControl());
		map.enableDoubleClickZoom();
		map.enableScrollWheelZoom();


		//window.setTimeout(setupPigeonMarkers, 0);	
		GEvent.addListener(map, "resize", function() {
			map.checkResize();
		});	
		GEvent.addListener(map, 'moveend', makeLink);
	}
}

function clickspot() {
	//
}

function makeLink() {
	var lat = map.getCenter().lat().toFixed(6);
	var lng = map.getCenter().lng().toFixed(6)
	var a="index.html?" 
	+ "lat=" + lat
	+ "&lng=" + lng
	+ "&zoom=" + map.getZoom();
	document.getElementById('link-to').value=a;
}

function createCustomProjection() {
	zoom = IN['zoom'];
	var projection = G_NORMAL_MAP.getProjection();
	projection.tileCheckRange = function(tile, zoom, tilesize) {
		var tileBounds = Math.pow(2, zoom);
		if (tile.y < 0 || tile.y >= tileBounds) {
			return false;
		}
		if (tile.x < 0 || tile.x >= tileBounds) {
			return false;
		}
		return true;
	}
	return projection;
}

function setupPigeonMarkers() {
	var mgrOptions = { maxZoom: 5, trackMarkers: true };
	mgr = new MarkerManager(map, mgrOptions);

	$.getJSON('data.txt',function(data) {
		var markers=[];
		for(i=0;i<data.points.pigeon.length;i++) {
			var guyIcon=new GIcon(G_DEFAULT_ICON);
			guyIcon.image="images/pigeon.png";
			guyIcon.iconSize=new GSize(16,16);
			guyIcon.shadow     = null;
	        guyIcon.shadowSize = new GSize(0,0);
	        guyIcon.iconAnchor = new GPoint(guyIcon.iconSize.width/2, guyIcon.iconSize.height/2);
	        guyIcon.infoWindowAnchor = new GPoint(guyIcon.iconSize.width/2, 10);
	        
			var marker=createMarker(map,new GLatLng(data.points.pigeon[i].lat,data.points.pigeon[i].lng),guyIcon,"<img src=\""+data.points.pigeon[i].imgT+"\" alt=\"\" border=\"0\" />");
			markers.push(marker);
		}
		mgr.addMarkers(markers,1,5);
		mgr.refresh();
	});

}

function setupStuntMarkers() {
	var mgrOptions={ maxZoom: 5,trackMarkers: true };
	mgrStunt=new MarkerManager(map,mgrOptions);

	$.getJSON('data.txt',function(data) {
		var markers=[];
		for(i=0;i<data.points.stunt.length;i++) {
			var guyIcon=new GIcon(G_DEFAULT_ICON);
			guyIcon.image="images/stunt.png";
			guyIcon.iconSize=new GSize(16,16);
			guyIcon.shadow=null;
			guyIcon.shadowSize=new GSize(0,0);
			guyIcon.iconAnchor=new GPoint(guyIcon.iconSize.width/2,guyIcon.iconSize.height/2);
			guyIcon.infoWindowAnchor=new GPoint(guyIcon.iconSize.width/2,10);

			var marker=createMarker(map,new GLatLng(data.points.stunt[i].lat,data.points.stunt[i].lng),guyIcon,"");
			markers.push(marker);
		}
		mgrStunt.addMarkers(markers,1,5);
		mgrStunt.refresh();
	});

}

function setupPlatformMarkers() {
	var mgrOptions={ maxZoom: 5,trackMarkers: true };
	mgrPlatform=new MarkerManager(map,mgrOptions);

	$.getJSON('data.txt',function(data) {
		var markers=[];
		for(i=0;i<data.points.platform.length;i++) {
			var guyIcon=new GIcon(G_DEFAULT_ICON);
			guyIcon.image="images/platform.png";
			guyIcon.iconSize=new GSize(16,16);
			guyIcon.shadow=null;
			guyIcon.shadowSize=new GSize(0,0);
			guyIcon.iconAnchor=new GPoint(guyIcon.iconSize.width/2,guyIcon.iconSize.height/2);
			guyIcon.infoWindowAnchor=new GPoint(guyIcon.iconSize.width/2,10);

			var marker=createMarker(map,new GLatLng(data.points.platform[i].lat,data.points.platform[i].lng),guyIcon,"<img src=\""+data.points.platform[i].imgT+"\" alt=\"\" border=\"0\" />");
			markers.push(marker);
		}
		mgrPlatform.addMarkers(markers,1,5);
		mgrPlatform.refresh();
	});

}

function setupWeaponMarkers() {
	var mgrOptions={ maxZoom: 5,trackMarkers: true };
	mgrWeapon=new MarkerManager(map,mgrOptions);

	$.getJSON('data.txt',function(data) {
		var markers=[];
		for(i=0;i<data.points.weapon.length;i++) {
			var guyIcon=new GIcon(G_DEFAULT_ICON);
			guyIcon.image="images/weapon_cur.png";
			guyIcon.iconSize=new GSize(16,16);
			guyIcon.shadow=null;
			guyIcon.shadowSize=new GSize(0,0);
			guyIcon.iconAnchor=new GPoint(guyIcon.iconSize.width/2,guyIcon.iconSize.height/2);
			guyIcon.infoWindowAnchor=new GPoint(guyIcon.iconSize.width/2,10);

			var marker=createMarker(map,new GLatLng(data.points.weapon[i].lat,data.points.weapon[i].lng),guyIcon,data.points.weapon[i].text);
			markers.push(marker);
		}
		mgrWeapon.addMarkers(markers,1,5);
		mgrWeapon.refresh();
	});

}

function setupVehicleMarkers() {
	var mgrOptions={ maxZoom: 5,trackMarkers: true };
	mgrVehicle=new MarkerManager(map,mgrOptions);

	$.getJSON('data.txt',function(data) {
		var markers=[];
		for(i=0;i<data.points.theft.length;i++) {
			var guyIcon=new GIcon(G_DEFAULT_ICON);
			guyIcon.image="images/vehicle_cur.png";
			guyIcon.iconSize=new GSize(16,16);
			guyIcon.shadow=null;
			guyIcon.shadowSize=new GSize(0,0);
			guyIcon.iconAnchor=new GPoint(guyIcon.iconSize.width/2,guyIcon.iconSize.height/2);
			guyIcon.infoWindowAnchor=new GPoint(guyIcon.iconSize.width/2,10);

			var marker=createMarker(map,new GLatLng(data.points.theft[i].lat,data.points.theft[i].lng),guyIcon,"<img src=\""+data.points.theft[i].imgT+"\" alt=\"\" />"+data.points.theft[i].text);
			markers.push(marker);
		}
		mgrVehicle.addMarkers(markers,1,5);
		mgrVehicle.refresh();
	});

}

function setupCharacterMarkers() {
	var mgrOptions={ maxZoom: 5,trackMarkers: true };
	mgrCharacter=new MarkerManager(map,mgrOptions);

	$.getJSON('data.txt',function(data) {
		var markers=[];
		for(i=0;i<data.points.character.length;i++) {
			var guyIcon=new GIcon(G_DEFAULT_ICON);
			guyIcon.image="images/character_cur.png";
			guyIcon.iconSize=new GSize(16,16);
			guyIcon.shadow=null;
			guyIcon.shadowSize=new GSize(0,0);
			guyIcon.iconAnchor=new GPoint(guyIcon.iconSize.width/2,guyIcon.iconSize.height/2);
			guyIcon.infoWindowAnchor=new GPoint(guyIcon.iconSize.width/2,10);

			var marker=createMarker(map,new GLatLng(data.points.character[i].lat,data.points.character[i].lng),guyIcon,"<img src=\""+data.points.character[i].imgT+"\" alt=\"\" />"+data.points.character[i].text);
			markers.push(marker);
		}
		mgrCharacter.addMarkers(markers,1,5);
		mgrCharacter.refresh();
	});

}

function setupMissionMarkers() {
	var mgrOptions={ maxZoom: 5,trackMarkers: true };
	mgrMission=new MarkerManager(map,mgrOptions);

	$.getJSON('data.txt',function(data) {
		var markers=[];
		for(i=0;i<data.points.activities.length;i++) {
			var guyIcon=new GIcon(G_DEFAULT_ICON);
			guyIcon.image="images/mission_cur.png";
			guyIcon.iconSize=new GSize(16,16);
			guyIcon.shadow=null;
			guyIcon.shadowSize=new GSize(0,0);
			guyIcon.iconAnchor=new GPoint(guyIcon.iconSize.width/2,guyIcon.iconSize.height/2);
			guyIcon.infoWindowAnchor=new GPoint(guyIcon.iconSize.width/2,10);

			var marker=createMarker(map,new GLatLng(data.points.activities[i].lat,data.points.activities[i].lng),guyIcon,data.points.activities[i].label);
			markers.push(marker);
		}
		mgrMission.addMarkers(markers,1,5);
		mgrMission.refresh();
	});

}

function createMarker(map, latlng, ico, content) {
	var marker=new GMarker(latlng,{ icon: ico });

	if(content!="") {
		GEvent.addListener(marker,"click",function() {
			map.closeInfoWindow();
			map.panTo(this.getLatLng());
			this.openInfoWindowHtml(content);
		});
	}

  return marker;
}